<template>
  <div class="cont">
    <!-- 搜索工作栏 -->
    <el-form ref="queryFormRef" :inline="true" :model="queryParams" class="cont_top" label-width="74px"
      @submit.prevent="handleQuery">
      <el-form-item style="margin-right: 12px">
        <el-button link type="primary" @click="addbtn" class="btnhover" style="
            width: 90px;
            height: 30px;
            background-color: rgba(218, 111, 40, 1);
            color: rgba(255, 255, 255, 1);
          ">新增网格</el-button>
      </el-form-item>
      <el-form-item label="网格名称" prop="gridName" style="margin-right: 17px">
        <el-input v-model="queryParams.gridName" class="!w-240px" placeholder="" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item style="margin-right: 0">
        <el-button @click="handleQuery" class="btn btnsearch">查询</el-button>
        <el-button @click="resetQuery" class="btn btnreset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table v-loading="loading" element-loading-text="Loading..." :data="list" row-key="id" default-expand-all
      style="margin-bottom: 20px; min-height: 435px" :header-cell-style="{ background: '#F0F0F0' }">
      <el-table-column align="center" show-overflow-tooltip label="网格名称" min-width="300">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.gridName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="category" label="网格类型" show-overflow-tooltip align="center" min-width="160">
        <template #default="{ row }">
          <div class="namecont">
            {{ typeRelate(row.category) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column :formatter="dateFormatter" align="center" label="创建时间" prop="createTime" min-width="200" />
      <el-table-column align="center" show-overflow-tooltip label="创建人" prop="creator" min-width="180">
        <template #default="scope">
          <div class="namecont">
            {{ scope.row.creator }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button link type="primary" class="btnhover" @click="handleAudit(scope.row)"
            style="color: #3c88f0">编辑</el-button>
          <el-button link type="primary" class="btnhover" @click="delbtn(scope.row)"
            style="color: #3c88f0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination style="font-size: 14px" v-model:current-page="queryParams.pageNo" :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize" :page-sizes="pageSizes" layout="total,sizes,prev, pager, next,jumper"
      :total="total" @size-change="selectchange" @current-change="handleCurrentChange" />
  </div>
  <EditDialog v-model:visible="editDialogVisible" @getisShow="getisShow" :editData="editData"
    @geteditData="geteditData" />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { dateFormatter } from '@/utils/formatTime'
import EditDialog from './components/editDialog.vue'
import { useRouter } from 'vue-router'
import * as getgridApi from '@/api/countryside/gridmanagement/index'
import globalApi from '@/api/spb/global'
const { push } = useRouter() // 路由
const loading = ref(true) // 列表加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const pageSizes = [50, 80, 100]
const queryParams = reactive({
  pageNo: 1,
  pageSize: 50,
  gridName: ''
})
const queryFormRef = ref() // 搜索的表单
const editDialogVisible = ref(false)
const editData = ref({})
const getisShow = (val) => {
  editDialogVisible.value = val
}
const geteditData = (val) => {
  handleCurrentChange(val)
}
/** 获取类型列表 */
const optiontype = ref([])
const getTypeData = () => {
  const params = {
    pageNo: 1,
    pageSize: 100,
    dictType: 'countryside_grid_area_type'
  }
  globalApi.getDictData(params).then((res) => {
    optiontype.value = res.list
  })
}
// 分类字段映射
const typeRelate = (item) => {
  let content = ''
  optiontype.value.forEach((t) => {
    if (t.value == item) {
      content = t.label
    }
  })
  return content
}

/** 条数变化 */
const selectchange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}
/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}

/** 查询任务列表 */
const getList = async () => {
  getgridApi
    .getGridPage(queryParams)
    .then((res) => {
      let cloneData = JSON.parse(JSON.stringify(res.list)) // 对源数据深度克隆
      cloneData = cloneData.filter((father) => {
        // 循环所有项，并添加children属性
        let branchArr = cloneData.filter((child) => father.id == child.parentId) // 返回每一项的子级数组
        branchArr.length > 0 ? (father.children = branchArr) : '' //给父级添加一个children属性，并赋值
        return father.parentId == 0 //返回第一层
      })
      loading.value = false
      if (cloneData.length == 0) {
        list.value = res.list
      } else {
        list.value = cloneData
      }
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      total.value = res.total
    })
    .catch((error) => {
      loading.value = false
    })
}
/** 新增 */
const addbtn = () => {
  editDialogVisible.value = true
  editData.value = {}
  editData.value.dialogtitle = '新增网格'
  editData.value.pageNo = 1
}

/** 编辑 */
const handleAudit = (row: any) => {
  editDialogVisible.value = true
  editData.value = row
  editData.value.category = JSON.stringify(row.category)
  editData.value.parentId = row.parentId
  editData.value.dialogtitle = '编辑网格'
  editData.value.pageNo = queryParams.pageNo
}

/** 删除 */
const delbtn = (row: any) => {
  ElMessageBox.confirm(
    `确认删除【${row.gridName}】吗？删除当前网格会删除当前网格下所有网格员、房屋、村民、党员和领导班子，请谨慎操作！`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'error'
    }
  )
    .then(() => {
      getgridApi.deleteGrid(row.id).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

/** 查询按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getgridApi
    .getGridPage(queryParams)
    .then((res) => {
      list.value = res.list
      total.value = res.total
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
    })
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.pageNo = 1
  getList()
}

/** 初始化 **/
onMounted(() => {
  getTypeData()
  getList()
})
</script>

<style scoped>
.cont {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: center;
  min-height: 510px;
  position: relative;
}

.cont_top {
  text-align: left;
  margin: 4px 0;
}

::v-deep .el-table .cell {
  display: flex;
  justify-content: center !important;
  align-items: center;
}

::v-deep .el-table .cell:first-child {
  justify-content: left !important;
}

::v-deep .el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #cc352f;
}

::v-deep .el-select__wrapper.is-focused {
  box-shadow: 0 0 0 1px #cc352f;
}

.btn {
  width: 80px;
  height: 30px;
  line-height: 20px;
  border-radius: 5px;
  font-size: 14px;
  text-align: center;
}

.btnreset {
  background-color: rgba(255, 255, 255, 1);
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(142, 142, 142, 1);
}

.btnsearch {
  background-color: #cc352f;
  color: rgba(255, 255, 255, 1);
  border: 1px solid #cc352f;
}

.btn:hover {
  opacity: 0.8;
}

.btnhover:hover {
  opacity: 0.8;
}

.el-pagination {
  justify-content: center;
}

.namecont {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

::v-deep .el-form-item__label {
  padding: 0 17px 0 0;
}
</style>
